﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// jixie_category 的摘要说明
    /// </summary>
    public class jixie_list : IHttpHandler
    {
        protected int totalCount = 0;
        protected int page = 1;
        protected int pageSize = 0;
        public void ProcessRequest(HttpContext context)
        { //取得处事类型
            string action = DTRequest.GetQueryString("action");

            switch (action)
            {
                case "get_category_list": //获得分类
                    get_category_list(context);
                    break;
                case "get_jixie": //获得机械用品
                    get_jixie(context);
                    break;
             
            }
            
        }
        private void get_category_list(HttpContext context) {
            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            //获取供应信息
            BLL.jixie_category bll = new BLL.jixie_category();
            DataSet ds = bll.GetList(99, "1=1", "sort_id asc");
            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"jixie_category_list\":[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"title\":\"" + dr["title"] + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            strTxt.Append("}");

            context.Response.Write(strTxt.ToString());
        }
        private void get_jixie(HttpContext context)
        {
            this.pageSize = 15;
            this.page = DTRequest.GetQueryInt("page", 1);
            int category_id = DTRequest.GetQueryInt("category_id", 0);
            pageSize = DTRequest.GetQueryInt("pageSize", 15);
            StringBuilder strTxt = new StringBuilder();

            strTxt.Append("{");
            BLL.jixie bll = new BLL.jixie();
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append("id>0");
            if (category_id > 0)
            {
                strWhere.Append(" and category_id=" + category_id);
            }
            DataSet ds = bll.GetList(this.pageSize,this.page, strWhere.ToString(), "sort_id asc,add_time desc", out this.totalCount);
            decimal t_page = Math.Ceiling(decimal.Parse((this.totalCount / this.pageSize).ToString()));
            int total_page = int.Parse(t_page.ToString());
            strTxt.Append("\"total_page\":" + total_page.ToString() + ",");
            if (ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"jixie_list\":[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    string _title = dr["title"].ToString();
                    if (_title.Length > 7)
                    {
                        strTxt.Append(",\"title\":\"" + _title.Substring(0, 7) + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"title\":\"" + _title + "\"");
                    }
                    if (Convert.ToInt32(dr["price"]) > 0)
                    {
                        strTxt.Append(",\"market_price\":\"" + dr["price"] + "元/" + dr["unit"] + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"market_price\":\"面议\"");
                    }
                    string proimg = dr["img_url"].ToString();

                    if (string.IsNullOrEmpty(proimg))
                    {
                        strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
                    }
                    else
                    {
                        if (proimg.Contains("shicaiquanzi"))
                        {
                            if (proimg.Contains("http://")) { strTxt.Append(",\"img_url\":\"" + proimg + "\""); }
                            else { strTxt.Append(",\"img_url\":\"http://" + proimg + "\""); }

                        }
                        else
                        {
                            strTxt.Append(",\"img_url\":\"http://pic.shicaiquanzi.com" + proimg + "\"");
                        }
                    }

                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            else {
                context.Response.Write("{\"status\":0, \"msg\":\"暂无机械产品！\"}");
            }
            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}